<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="handleAdd">新建</a-button>
      <!-- <router-link :to="{ name: 'AdminMenuList' }" tag="button" class="ant-btn ant-btn-default" v-if="pid">返回上一级</router-link> -->
    </div>
    <a-table
      :columns="columns"
      :data-source="data"
      :row-key="
        (record) => {
          return record.id
        }
      "
      :loading="loading"
      :pagination="false"
    >
      <span slot="admin" slot-scope="text, record">
        <template>
          <template v-if="record.parentId == 0">
            <a @click="addChild(record)">添加子权限</a>
            <a-divider type="vertical" />
          </template>
          <a @click="handleEdit(record)">修改</a>
          <a-divider type="vertical" />
          <a @click="deleteOne(record.id)">删除</a>
        </template>
      </span>
    </a-table>
    <admin-form ref="createModal" :visible="visible" :model="mdl" @cancel="handleCancel" @ok="handleOk" />
  </a-card>
</template>

<script>
import AdminForm from './modules/AdminForm.vue'
const columns = [
  {
    title: '权限名称',
    dataIndex: 'authorityName'
  },
  {
    title: '控制器',
    dataIndex: 'controller'
  },
  {
    title: '功能操作',
    dataIndex: 'action'
  },
  {
    title: '管理',
    dataIndex: 'admin',
    scopedSlots: { customRender: 'admin' }
  }
]
export default {
  components: {
    AdminForm
  },
  data() {
    return {
      columns,
      visible: false,
      mdl: {},
      data: [],
      loading: false
    }
  },
  created() {
    this.getPageData()
  },
  methods: {
    // 添加
    handleAdd() {
      this.mdl = { sort: 1 }
      this.visible = true
    },
    handleOk() {
      this.visible = false
      this.getPageData()
    },
    handleCancel() {
      this.visible = false
    },
    // 获取页面数据
    getPageData() {
      this.loading = true
      this.$common.handlePost('/authorities/getListAll', {}).then((data) => {
        this.loading = false
        if (data.code === 200) {
          console.log(data)
          const result = data.result
          this.data = result
        }
      })
    },
    deleteOne(id) {
      this.handleDelete(id)
    },
    handleDelete(deleteId) {
      const _this = this
      const params = {
        id: deleteId
      }
      this.$common
        .deleteOne('/authorities/delete/' + deleteId, params, deleteId)
        .then((res) => {
          if (res) {
            _this.getPageData()
          }
        })
        .catch(() => {
          this.$notification.error({
            message: '错误',
            description: '请求信息失败，请重试'
          })
        })
    },
    // 修改
    handleEdit(record) {
      this.$common.handlePost('/authorities/getInfo/' + record.id).then((data) => {
        if (data.code === 200) {
          this.visible = true
          const result = data.result
          this.mdl = result
          this.mdl.parentId = result.parentId || 0
        } else {
          this.$message.error(data.message)
        }
      })
    },
    // 添加子菜单
    addChild(record) {
      // id: 这条记录id  parentId: 这条记录的父id
      this.mdl = { parentId: record.id, controller: record.controller, sort: 1 }
      this.visible = true
    }
  }
}
</script>
